// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

// import Tool from "../resources/framework/Tool";
// import sceneMediator from "../resources/framework/sceneMediator";
import value = cc.js.value;
import Tool from "../framework/Tool";
import sceneMediator from "../framework/sceneMediator";
import GameConfig from "../framework/GameConfig";
import mainManage from "../framework/mainManage";

export enum TraverStatus {
    unBuild = 0,
    building = 1,
    open = 2,
    close = 3
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class gameData {

    public static processPumpkinNumber = 0;

    public static processPumpkinDate = [];

    public static levelPumpkinDate = [];

    public static pumpkinShowDate = [];

    public static titleStringList = [];

    public static titleDate = [];

    public static princeTitleDate = [];

    public static roleDate = null;

    public static petDate = null;

    public static curTitlePart = 0;

    public static sceneReversal = false;

    public static petSkinList = [0];

    public static curUseSkin = [];

    public static roleSkinList = [1];

    public static curUseRoleSkin = 0;

    public static passEasterEgg = false;

    public static keyUsingIndex = -1;

    public static randomSkinIndex = -1;

    public static isRelive = false;

    public static isRestart = false;

    public static isShowCheats = 0;

    public static showQuestion = [];

    public static clickShowNew = false;

    public static lastColorfulPart = 0;

    public static showMainView = 0;

    public static failGameData = 0;

    public static colorfulPiece = [
        [1,1,1,1,0,0,0],
        [0,0,0,0,0],
        [0,0,0,0,0,0,0],
        [0,0],
        [0,0,0],
        [0,0],
        [0]
    ];

    public static eggWallDate = [];

    public static skinWallDate = [];

    public static backToGame = false;

    public static rebackIsSuccess = false;

    public static titleFreeTimes = 2;

    public static privacyAgreementShow = 0;


    /* ================================================================================*/


    /* ============================== 场景反转 ========================================*/
    static setSceneReversal(value){
        this.sceneReversal = value;
    }

    static getSceneReversal(){
        return this.sceneReversal;
    }

    /* ================================== 游戏提示 ======================================*/
    static setCurTitlePart(value){
        this.curTitlePart = value;
    }

    static clearCurTitle(){
        this.curTitlePart = 0;
        this.keyUsingIndex = -1;
        if(!gameData.isRelive){
            gameData.randomSkinIndex = -1;
        }
    }

    public static getTitleData(scene,part?){
        this.curTitlePart++;
        let stringList = this.titleDate[scene][this.curTitlePart - 1];
        if(typeof part != "undefined"){
            stringList = this.titleDate[scene][part];
        }
        if(typeof stringList == "undefined"){
            stringList = "";
        }
        return stringList;
    }

    static getCurTitle(){
        return this.curTitlePart;
    }

    static getCurTitleString(){
        return this.titleStringList;
    }

    /* ================================= 游戏初始数据 ==============================================*/
    static rewardList = null;
    static analysisLocalJsonInformation(){
        Tool.loadDataFromJson("hint_full",(res)=>{
            let levelArr = [];
            for(let i in res){
                if(levelArr.indexOf(res[i]["stage"]) < 0){
                    levelArr.push(res[i]["stage"]);
                }
            }
            console.log(levelArr[levelArr.length - 1]);
            let titleArr = [];
            for(let i = 0 ; i < levelArr[levelArr.length - 1] + 1 ; i++){
                let arr = [];
                for(let j in res){
                    if(res[j]["stage"] == null || res[j]["stage"] == undefined){
                        continue;
                    }else{
                        if(res[j]["stage"] == i){
                            arr.push(res[j]["text"]);
                        }
                    }
                }
                titleArr.push(arr);
            }
            this.titleDate = titleArr;
            console.log("提示标语",this.titleDate);
        });

        Tool.loadDataFromJson("apple_full",(res)=>{
            let arr = [];
            for(let i in res){
                arr.push(res[i]["num"]);
            }
            this.levelPumpkinDate = arr;

            let data = Tool.getData("pumpkinShowDate");
            if(data != null && data != "" && data != undefined){
                this.pumpkinShowDate = JSON.parse(data);
                console.log(this.pumpkinShowDate.length);
                for(let i in this.levelPumpkinDate){
                    if(parseInt(i) < this.pumpkinShowDate.length) continue;
                    let arr1 = [];
                    for(let j = 0; j < this.levelPumpkinDate[i];j++){
                        arr1.push(1);
                    }
                    this.pumpkinShowDate.push(arr1);
                }
            }else{
                for(let i in this.levelPumpkinDate){
                    let arr1 = [];
                    for(let j = 0; j < this.levelPumpkinDate[i];j++){
                        arr1.push(1);
                    }
                    this.pumpkinShowDate.push(arr1);
                }
            }
            console.log(this.pumpkinShowDate.length);
            console.log("苹果数",this.levelPumpkinDate);
            console.log("苹果展示数据",this.pumpkinShowDate);
        });

        Tool.loadDataFromJson("hint2_full",(res)=>{
            let arr = [];
            for(let i in res){
                if(parseInt(i) >= 50){
                    this.princeTitleDate.push(res[i]["text"]);
                }else{
                    arr.push(res[i]["text"]);
                }
            }
            this.titleStringList = arr;
            console.log("提示",this.titleStringList);
            console.log("魔王提示",this.princeTitleDate);
        });
    }

    /* ============================ 游戏中苹果相关数据 =============================*/
    public static savePumpkinDate(){
        let data = JSON.stringify(this.processPumpkinDate);
        Tool.saveData("PumpkinDates",data);
    }

    public static getLocalEveryLevelAppleInformation(){
        let data = Tool.getData("PumpkinDates");
        if(data != null && data != "" && data != undefined){
            this.processPumpkinDate = JSON.parse(data);
            for(let i = 0 ; i < 60 - this.processPumpkinDate.length ; i++){
                this.processPumpkinDate.push(0);
            }
        }else{
            for(let i = 0 ; i < 60 ;i++){
                this.processPumpkinDate.push(0);
            }
        }
    }

    static getPumpkinShowDate(){
        return this.pumpkinShowDate;
    }

    public static savePumpkinShowDate(){
        Tool.saveData("pumpkinShowDate",JSON.stringify(this.pumpkinShowDate));
    }

    static getProcessPumpkin(){
        return this.processPumpkinDate;
    }

    static getLevelPumpkin(){
        return this.levelPumpkinDate;
    }

    static savePumpkinNumber(){
        Tool.saveData("pumpkinNumber",this.processPumpkinNumber);
    }

    static getLocalTotalAppleInformation(){
        let data = Tool.getData("pumpkinNumber");
        if(data != null && data != "" && data != undefined){
            this.processPumpkinNumber = parseInt(data);
        }else{
            this.processPumpkinNumber = 0;
        }
    }

    static getCurPumpkinNumber(){
        return this.processPumpkinNumber;
    }

    static costPumpkinNumber(value){
        this.processPumpkinNumber -= value;
        this.savePumpkinNumber();
    }

    static addPumpkin(){
        let scene = 0;
        if(sceneMediator.curEnterLevel == 0){
            scene = sceneMediator.getShowScene();
        }else{
            scene = 50 + sceneMediator.princeShowLevel;
        }
        if(scene <= this.processPumpkinDate.length){
            if(this.processPumpkinDate[scene - 1] < this.levelPumpkinDate[scene - 1]){
                this.processPumpkinDate[scene - 1] += 1;
            }
        }else{
            this.processPumpkinDate.push(1);
        }
        this.savePumpkinDate();
        this.processPumpkinNumber++;
        this.savePumpkinNumber();
    }

    static addSinglePumpkin(index?){
        if(index){
            this.processPumpkinNumber += index;
        }else{
            this.processPumpkinNumber++;
        }
        this.savePumpkinNumber();
    }

    public static showRewardApple = false;
    static getLocalShowRewardAppleInformation(){
        let data = Tool.getData("showRewardApple");
        if(data != null && data != "" && data != undefined){
            if(parseInt(data) == 0){
                this.showRewardApple = false;
            }else{
                this.showRewardApple = true;
            }
        }else{
            this.showRewardApple = false;
        }
    }

    static saveShowRewardApple(){
        this.showRewardApple = true;
        Tool.saveData("showRewardApple",1);
    }
}
